package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.admin.util.UploadUtil;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyDetailQuery;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by 13925 on 2018/10/29.
 */

/**
 * 用户游记管理层
 */
@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {

    @Autowired
    IStrategyDetailService strategyDetailService;

    @Autowired
    IStrategyCatalogService strategyCatalogService;

    @Autowired
    IStrategyService strategyService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyDetailQuery qo, Model model) {
        qo.setOrderBy("s.releaseTime DESC");
        PageInfo result = strategyDetailService.query(qo);
        model.addAttribute("result", result);
        model.addAttribute("strategys", strategyService.selectAll());
        return "strategy/detail";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(MultipartFile file, StrategyDetail strategyDetail) {
        //文件上传
        if (file != null && file.getSize() > 0) {
            String coverUrl = UploadUtil.upload(file, UploadUtil.BASE_IMAGE_PATH + "/upload");
            strategyDetail.setCoverUrl(coverUrl);
        }
        strategyDetailService.saveOrUpdate(strategyDetail);
        return "redirect:/strategyDetail/list.do";
    }

    @RequestMapping("/getCatalogByStrategyId")
    @ResponseBody
    public Object getCatalogByStrategyId(Long strategyId) {
        return strategyCatalogService.getCatalogByStrategyId(strategyId);
    }

    /**
     * 查询攻略内容
     * @param detailId
     * @return
     */
    @RequestMapping("/getContentByDetailId")
    @ResponseBody
    public Object getContentByDetailId(Long detailId) {
        return strategyDetailService.getContentByDetailId(detailId);
    }

}
